package club.banyuan.reserve.service;

import club.banyuan.reserve.consts.AppConst;
import club.banyuan.reserve.dto.BookedParam;
import club.banyuan.reserve.mapper.OrdersMapper;
import club.banyuan.reserve.mapper.PositionMapper;
import club.banyuan.reserve.model.Orders;
import club.banyuan.reserve.model.OrdersExample;
import club.banyuan.reserve.model.Position;
import club.banyuan.reserve.model.PositionExample;
import club.banyuan.reserve.vo.BookHours;
import club.banyuan.reserve.vo.PositionRecordsVo;
import club.banyuan.reserve.vo.PositionVo;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HanChao
 * 描述信息：
 */
@Service
public class BookServiceImp implements BookService{

    @Autowired
    private PositionMapper positionMapper;

    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public List<PositionVo> showPositionList() {

        List<Position> positionList = positionMapper.selectByExample(new PositionExample());

        List<PositionVo> list = new ArrayList<>();

        for (Position p: positionList) {

            PositionVo positionVo = new PositionVo();
            positionVo.setId(p.getId());
            positionVo.setName(p.getName());
            positionVo.setStatus(p.getStatus());

            list.add(positionVo);
        }

        return list;
    }

    @Override
    public PositionRecordsVo showBookTimeList(Integer positionId) {

        // 根据positionId找到座位信息,存进PositionVo
        PositionExample positionExample = new PositionExample();
        positionExample.createCriteria()
                .andIdEqualTo(positionId);
        List<Position> positionList = positionMapper.selectByExample(positionExample);

        Position pos = positionList.get(0);

        PositionVo position = new PositionVo();
        position.setId(pos.getId());
        position.setName(pos.getName());
        position.setStatus(pos.getStatus());

        // 获取当前时间
        DateTime nowTime = DateTime.now();

        // 获取大于当前时间,未预定的订单
        OrdersExample ordersExample = new OrdersExample();
        ordersExample.createCriteria()
                .andPositionidEqualTo(positionId)
                .andCancelflagEqualTo(false)
                .andDateGreaterThanOrEqualTo(DateUtil.format(nowTime, AppConst.FORMAT_YMD));
        List<Orders> ordersList = ordersMapper.selectByExample(ordersExample);

        System.out.println(ordersList.size());

        // 处理一下格式
        List<String> orderCreateAt = new ArrayList<>();
        for (Orders o: ordersList) {

            String hour;
            if (o.getHour() < 10) {

                hour = "0" + o.getHour();
            } else {

                hour = o.getHour().toString();
            }

            String createAt = o.getDate() + " " + hour + ":00";
            orderCreateAt.add(createAt);
        }

        List<BookHours> bookHoursList = new ArrayList<>();
        for (int i = 1; i <= AppConst.BOOK_HOUR_LIMIT; i++) {

            BookHours bookHour = new BookHours();

            DateTime dateTime = DateUtil.offsetHour(nowTime,i);
            bookHour.setDate(DateUtil.format(dateTime,AppConst.FORMAT_YMD));
            bookHour.setHour(Integer.parseInt(DateUtil.format(dateTime,"HH")));

            String showBookTime = DateUtil.format(dateTime,AppConst.FORMAT_YMD_H) + ":00";

            if (orderCreateAt.contains(showBookTime)) {

                bookHour.setHasBooked(true);
            } else {

                bookHour.setHasBooked(false);
            }

            bookHoursList.add(bookHour);
        }

        // 返回给前端的列表
        PositionRecordsVo positionRecordsVo = new PositionRecordsVo();
        positionRecordsVo.setPosition(position);
        positionRecordsVo.setBookHours(bookHoursList);

        return positionRecordsVo;
    }

    @Override
    public void booking(List<BookedParam> params, Integer userId) {

        for (BookedParam b: params) {

            String[] s = b.getDate().split(" ");
            String date = s[1];

            Orders order = new Orders();
            order.setPositionid(b.getPositionId());
            order.setHour(b.getHours());
            order.setDate(date);
            order.setUserid(userId);
            order.setHasbooked(true);

            ordersMapper.insertSelective(order);
        }
    }

    @Override
    public void bookCancelByOrderId(Integer orderId) {

        Orders order = new Orders();
        order.setCancelflag(true);
        OrdersExample ordersExample = new OrdersExample();
        ordersExample.createCriteria()
                .andIdEqualTo(orderId);
        ordersMapper.updateByExampleSelective(order,ordersExample);
    }
}
